﻿using System.Text;

using Serilog;

namespace Tszy.Unlimited.Base.Infrastructure.Logging
{
    /// <summary>
    ///
    /// </summary>
    public class SerilogLogger : LoggerBase
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected override void Debug(string message, Exception? exception = null)
        {
            if (exception != null)
            {
                Log.Logger.Debug(exception, message);
            }
            else
            {
                Log.Logger.Debug(message);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected override void Error(string message, Exception? exception = null)
        {
            if (exception != null)
            {
                Log.Logger.Error(exception, message);
            }
            else
            {
                Log.Logger.Error(message);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected override void Fatal(string message, Exception? exception = null)
        {
            if (exception != null)
            {
                Log.Logger.Fatal(exception, message);
            }
            else
            {
                Log.Logger.Fatal(message);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected override void Information(string message, Exception? exception = null)
        {
            if (exception != null)
            {
                Log.Logger.Information(exception, message);
            }
            else
            {
                Log.Logger.Information(message);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="message"></param>
        /// <param name="exception"></param>
        protected override void Warning(string message, Exception? exception = null)
        {
            if (exception != null)
            {
                Log.Logger.Warning(exception, message);
            }
            else
            {
                Log.Logger.Warning(message);
            }
        }

        private static string GetException(Exception exception)
        {
            var messageBuilder = new StringBuilder();

            if (exception != null)
            {
                messageBuilder.AppendLine(exception.Message);
                messageBuilder.AppendLine(exception.StackTrace);

                if (exception.InnerException != null)
                {
                    messageBuilder.AppendLine(GetException(exception.InnerException));
                }
            }

            return messageBuilder.ToString();
        }
    }
}